<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户等级" prop="memberLevel">
        <el-select v-model="queryParams.memberLevel" placeholder="请选择用户等级，数据字典" clearable size="small">
          <el-option
            v-for="dict in memberLevelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别，数据字典" clearable size="small">
          <el-option
            v-for="dict in sexOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="潜水等级" prop="diveLevel">
        <el-select v-model="queryParams.diveLevel" placeholder="请选择潜水等级，数据字典" clearable size="small">
          <el-option
            v-for="dict in diveLevelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="最后登录时间" prop="lastLoginTime">
        <el-date-picker
              v-model="queryParams.lastLoginDateRange"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
      </el-form-item>
      <el-form-item label="注册时间" prop="registerTime">
        <el-date-picker
              v-model="queryParams.registerDateRange"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
      </el-form-item>
      <el-form-item label="国家" prop="countryId">
        <el-select v-model="queryParams.countryId" placeholder="请选择国家">
          <el-option
            v-for="country in countryList"
            :key="country.id"
            :label="country.name"
            :value="country.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="国际区号" prop="countryId">
        <el-select v-model="queryParams.countryId" placeholder="请选择">
          <el-option
            v-for="country in countryList"
            :key="country.id"
            :label="country.countryCode"
            :value="country.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['member:member:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['member:member:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['member:member:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['member:member:export']"
        >导出</el-button>
      </el-col>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery" />
        </el-tooltip>
        <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top">
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch" />
        </el-tooltip>
      </div>
    </el-row>

    <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="国家" align="center" prop="country.name" />
      <el-table-column label="国际区号" align="center" prop="country.countryCode" />
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column label="昵称" align="center" prop="nickname" />
      <el-table-column label="用户等级" align="center" prop="memberLevel" :formatter="memberLevelFormat" />
      <el-table-column label="最后登录时间" align="center" prop="lastLoginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastLoginTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" align="center" prop="registerTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registerTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账户状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="会员状态" align="center" prop="memberStatus" :formatter="memberStatusFormat" />
      <el-table-column label="会员到期时间" align="center" prop="memberExpireTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.memberExpireTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleRecord(scope.row)"
          >离线语音</el-button>
          <el-button
            v-show="scope.row.status == 1"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleFrozen(scope.row)"
            v-hasPermi="['member:member:frozen']"
          >冻结账户</el-button>
          <el-button
            v-show="scope.row.status == 2"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleThaw(scope.row)"
            v-hasPermi="['member:member:thaw']"
          >解冻账户</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="用户手机号1" prop="mobile1">
          <el-input v-model="form.mobile1" placeholder="请输入用户手机号1" />
        </el-form-item>
        <el-form-item label="用户手机号2" prop="mobile2">
          <el-input v-model="form.mobile2" placeholder="请输入用户手机号2" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="头像图片" prop="avatar">
          <el-input v-model="form.avatar" placeholder="请输入头像图片" />
        </el-form-item>
        <el-form-item label="当前会员卡id" prop="memberCardId">
          <el-input v-model="form.memberCardId" placeholder="请输入当前会员卡id" />
        </el-form-item>
        <el-form-item label="会员号码" prop="memberNum">
          <el-input v-model="form.memberNum" placeholder="请输入会员号码" />
        </el-form-item>
        <el-form-item label="用户等级，数据字典">
          <el-select v-model="form.memberLevel" placeholder="请选择用户等级，数据字典">
            <el-option
              v-for="dict in memberLevelOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="最后登录时间" prop="lastLoginTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.lastLoginTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择最后登录时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="注册时间" prop="registerTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.registerTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择注册时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="账户状态，数据字典">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="会员状态，数据字典">
          <el-radio-group v-model="form.memberStatus">
            <el-radio
              v-for="dict in memberStatusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="会员开始时间" prop="memberBeginTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.memberBeginTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择会员开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="会员到期时间" prop="memberExpireTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.memberExpireTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择会员到期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="性别，数据字典">
          <el-select v-model="form.sex" placeholder="请选择性别，数据字典">
            <el-option
              v-for="dict in sexOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="潜水等级，数据字典">
          <el-select v-model="form.diveLevel" placeholder="请选择潜水等级，数据字典">
            <el-option
              v-for="dict in diveLevelOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电子邮件" prop="email">
          <el-input v-model="form.email" placeholder="请输入电子邮件" />
        </el-form-item>
        <el-form-item label="所属行业，行业表" prop="industryId">
          <el-input v-model="form.industryId" placeholder="请输入所属行业，行业表" />
        </el-form-item>
        <el-form-item label="地址1" prop="address1">
          <el-input v-model="form.address1" placeholder="请输入地址1" />
        </el-form-item>
        <el-form-item label="地址2" prop="address2">
          <el-input v-model="form.address2" placeholder="请输入地址2" />
        </el-form-item>
        <el-form-item label="国家" prop="countryId">
          <el-select v-model="form.countryId" placeholder="请选择国家，数据字典">
            <el-option
              v-for="country in countryList"
              :key="country.id"
              :label="country.name"
              :value="country.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="邮编" prop="postcode">
          <el-input v-model="form.postcode" placeholder="请输入邮编" />
        </el-form-item>
        <el-form-item label="邮编" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入邮编" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="离线语音" :visible.sync="recordOpen">
      <el-table :data="recordList">
        <el-table-column property="id" label="序号" width="50"></el-table-column>
        <el-table-column label="时间" align="center" prop="startTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleDownloadRecord(scope.row)"
            >下载</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="recordTotal>0"
        :total="recordTotal"
        :page.sync="recordQueryParams.pageNum"
        :limit.sync="recordQueryParams.pageSize"
        @pagination="getRecordList"
      />
    </el-dialog>
  </div>
</template>

<script>
import { listMember, getMember, getMemberSobotRecord, delMember, addMember, updateMember, exportMember, frozenMember, thawMember } from "@/api/member/member";
import { listAllCountry } from "@/api/system/region";

export default {
  name: "Member",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员表格数据
      memberList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      recordOpen: false,
      recordList: [],
      recordTotal: 0,
      countryList: [],
      // 用户等级，数据字典字典
      memberLevelOptions: [],
      // 账户状态，数据字典字典
      statusOptions: [],
      // 会员状态，数据字典字典
      memberStatusOptions: [],
      // 性别，数据字典字典
      sexOptions: [],
      // 潜水等级，数据字典字典
      diveLevelOptions: [],
      lastLoginDateRange: [],
      registerDateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickname: null,
        memberLevel: null,
        sex: null,
        diveLevel: null,
        lastLoginBeginTime:null,
        lastLoginEndTime:null,
        registerBeginTime:null,
        registerEndTime:null
      },
      recordQueryParams:{
        pageNum: 1,
        pageSize: 10,
        id: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        memberLevel: [
          { required: true, message: "用户等级，数据字典不能为空", trigger: "blur" }
        ],
        lastLoginTime: [
          { required: true, message: "最后登录时间不能为空", trigger: "blur" }
        ],
        registerTime: [
          { required: true, message: "注册时间不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "注册时间不能为空", trigger: "blur" }
        ],
        delFlag: [
          { required: true, message: "注册时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("mem_member_level").then(response => {
      this.memberLevelOptions = response.data;
    });
    this.getDicts("mem_account_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("mem_member_status").then(response => {
      this.memberStatusOptions = response.data;
    });
    this.getDicts("sys_user_sex").then(response => {
      this.sexOptions = response.data;
    });
    this.getDicts("mem_member_dan_level").then(response => {
      this.diveLevelOptions = response.data;
    });
    listAllCountry().then(response => {
        this.countryList = response.data;
      });
  },
  methods: {
    /** 查询会员列表 */
    getList() {
      this.loading = true;
      this.queryParams.lastLoginBeginTime = null;
      this.queryParams.lastLoginEndTime = null;
      this.queryParams.registerBeginTime = null;
      this.queryParams.registerEndTime = null;
      if (null != this.lastLoginDateRange && '' != this.lastLoginDateRange) {
      	this.queryParams.lastLoginBeginTime = this.lastLoginDateRange[0];
      	this.queryParams.lastLoginEndTime = this.lastLoginDateRange[1];
      }
      if (null != this.registerDateRange && '' != this.registerDateRange) {
      	this.queryParams.registerBeginTime = this.registerDateRange[0];
      	this.queryParams.registerEndTime = this.registerDateRange[1];
      }
      listMember(this.queryParams).then(response => {
        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 用户等级，数据字典字典翻译
    memberLevelFormat(row, column) {
      return this.selectDictLabel(this.memberLevelOptions, row.memberLevel);
    },
    // 账户状态，数据字典字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 会员状态，数据字典字典翻译
    memberStatusFormat(row, column) {
      return this.selectDictLabel(this.memberStatusOptions, row.memberStatus);
    },
    // 性别，数据字典字典翻译
    sexFormat(row, column) {
      return this.selectDictLabel(this.sexOptions, row.sex);
    },
    // 潜水等级，数据字典字典翻译
    diveLevelFormat(row, column) {
      return this.selectDictLabel(this.diveLevelOptions, row.diveLevel);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        username: null,
        mobile1: null,
        mobile2: null,
        nickname: null,
        avatar: null,
        memberCardId: null,
        memberNum: null,
        memberLevel: null,
        lastLoginTime: null,
        registerTime: null,
        status: 0,
        memberStatus: 0,
        memberBeginTime: null,
        memberExpireTime: null,
        birthday: null,
        sex: null,
        diveLevel: null,
        email: null,
        industryId: null,
        address1: null,
        address2: null,
        country: null,
        postcode: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        nickname: null,
        memberLevel: null,
        sex: null,
        diveLevel: null,
        lastLoginBeginTime:null,
        lastLoginEndTime:null,
        registerBeginTime:null,
        registerEndTime:null
      }
      this.lastLoginDateRange = [];
      this.registerDateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员";
    },
    getRecordList(){
      getMemberSobotRecord(this.recordQueryParams).then(response =>{
        this.recordList = response.rows;
        this.recordOpen = true;
        this.recordTotal = response.total;
      });
    },
    handleRecord(row){
      this.recordList = [];
      this.recordQueryParams.id = row.id;
      this.getRecordList();
    },
    handleDownloadRecord(row){
      // window.open(process.env.VUE_APP_BASE_API + "/hospital/file/downloadFile?tPath=" + tPath)
      var name = "录音文件";
      var url = row.fileUrl;
      const a = document.createElement('a')
      a.setAttribute('download', name)
      a.setAttribute('target', '_blank')
      a.setAttribute('href', url)
      a.click()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMember(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMember(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addMember(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除会员编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMember(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有会员数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMember(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    handleFrozen(row){
      const ids = row.id || this.ids;
      this.$confirm('是否确认冻结会员编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return frozenMember(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("冻结成功");
        }).catch(function() {});
    },
    handleThaw(row){
      const ids = row.id || this.ids;
      this.$confirm('是否确认解冻会员编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return thawMember(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("冻结成功");
        }).catch(function() {});
    }
  }
};
</script>
